<style lang="less">
    .action-cell {
        margin-bottom: 20px;
        width: 100%;
        display: -webkit-box;
        display: -ms-flexbox;
        display: flex;
        -ms-flex-pack: distribute;
        justify-content: space-between;
        align-items: center;
        margin-right: 20%;
    }

    .search-block {
        display: flex;
        margin-bottom: 10px;
    }

    .input-search {
        margin-right: 10px;
    }
</style>
<template>
    <div style="min-width: 1024px">
        <h4>门区控制器</h4>
        <Alert show-icon>
            此处用于门区控制器。
        </Alert>
        <ButtonGroup>
            <Button type="success" @click="addModal=true">
                <Icon type="md-add-circle"/>
                添加
            </Button>
            <Button type="warning" :disabled="deleteBtnEnable" @click="remove">
                <Icon type="md-close-circle"/>
                删除
            </Button>
            <Button type="info" @click="backGoJump()">
                <Icon type="md-return-left"/>
                返回子系统管理
            </Button>
        </ButtonGroup>

        <Row>
            <Col span="20">
                <Table :context="self" :data="tableData" :columns="tableColumns" stripe border
                       @on-select="tableItemChecked" @on-select-cancel="tableItemChecked"
                       @on-select-all="tableItemChecked"
                       @on-select-all-cancel="tableItemChecked"
                       style="margin-top:15px;">
                </Table>
            </Col>
        </Row>
        <!--添加-->
        <Modal v-model="addModal" title="添加" @on-ok="handleSubmitAdd('formValidate')"
               @on-cancel="addModal =false">
            <Form ref="formValidate" :model="formValidate" :label-width="100">
                <Form-item label="控制器名称" prop="controllername">
                    <i-input v-model="formValidate.controllername" placeholder="请输入控制器名称"></i-input>
                </Form-item>
                <Form-item label="控制器IP" prop="controllerip">
                    <i-input v-model="formValidate.controllerip" placeholder="请输入控制器IP"></i-input>
                </Form-item>
                <Form-item label="控制器端口号" prop="controllerport">
                    <i-input v-model="formValidate.controllerport" placeholder="请输入控制器端口号"></i-input>
                </Form-item>
                <Form-item label="是否播放声音" prop="isplaysound">
                    <Col span="6">
                        <i-select v-model="formValidate.isplaysound" placeholder="请选择是否播放声音">
                            <i-option value="true">是</i-option>
                            <i-option value="false">否</i-option>
                        </i-select>
                    </Col>
                </Form-item>
                <Form-item label="备注说明" prop="comment">
                    <i-input v-model="formValidate.comment" placeholder="请输入备注说明"></i-input>
                </Form-item>
            </Form>
        </Modal>
        <!--编辑-->
        <Modal v-model="editIsShow" title="编辑" @on-ok="handleSubmitEdit('formValidate2')"
               @on-cancel="editIsShow =false">
            <Form ref="formValidate2" :model="formValidate2" :label-width="100">
                <Form-item label="控制器名称" prop="controllername">
                    <i-input v-model="formValidate2.controllername" placeholder="请输入控制器名称"></i-input>
                </Form-item>
                <Form-item label="控制器IP" prop="controllerip">
                    <i-input v-model="formValidate2.controllerip" placeholder="请输入控制器IP"></i-input>
                </Form-item>
                <Form-item label="控制器端口号" prop="controllerport">
                    <i-input v-model="formValidate2.controllerport" placeholder="请输入控制器端口号"></i-input>
                </Form-item>
                <Form-item label="是否播放声音" prop="isplaysound">
                    <Col span="6">
                        <i-select v-model="formValidate2.isplaysound" placeholder="请选择是否播放声音">
                            <i-option value="true">是</i-option>
                            <i-option value="false">否</i-option>
                        </i-select>
                    </Col>
                </Form-item>
                <Form-item label="备注说明" prop="comment">
                    <i-input v-model="formValidate2.comment" placeholder="请输入备注说明"></i-input>
                </Form-item>
            </Form>
        </Modal>

    </div>
</template>
<script>
    import apiUtil from '../../assets/utils/api_util';

    export default {
        data() {
            return {
                self: this,
                gateId: '',
                tableData: [],
                total: 0,
                current: 1,
                //添加
                formValidate: {
                    //添加字段
                    controllername: '',
                    controllerip: '',
                    controllerport: '',
                    isplaysound: '',
                    comment: '',
                },
                allDay: false,
                addModal: false,
                //编辑
                formValidate2: {
                    //添加字段
                    controllername: '',
                    controllerip: '',
                    controllerport: '',
                    isplaysound: '',
                    comment: '',
                },
                areaArr: [],
                editIsShow: false,
                //操作按钮状态管理
                deleteBtnEnable: true,
                selectedItems: [],
                tableColumns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '控制器名称',
                        key: 'Controllername',
                        align: 'center',

                    },
                    {
                        title: '控制器IP',
                        key: 'Controllerip',
                        align: 'center',
                    },
                    {
                        title: '控制器端口号',
                        key: 'Controllerport',
                        align: 'center',
                    },
                    {
                        title: '是否播放声音',
                        key: 'isplaysound',
                        align: 'center',
                        render: (h, params) => {
                            return h('span', params.row.Isplaysound ? '播放' : '不播放')
                        }
                    },
                    {
                        title: '备注',
                        key: 'Comment',
                        align: 'center',
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: '',
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            'margin-right': '10px',
                                        },
                                        on: {
                                            click: () => {
                                                let that = this;
                                                this.editClick(params.row)
                                            }
                                        }
                                    }, '编辑'),
                                    h('Button', {
                                        props: {
                                            type: 'warning',
                                            size: 'small'
                                        },
                                        style: {
                                            'margin-right': '10px',
                                        },
                                        on: {
                                            click: () => {
                                                let that = this;
                                                this.jumpPage(params.row.Id, 'GateCardList')
                                            }
                                        }
                                    }, '读卡器管理'),
                                ]
                            )
                        }
                    }
                ]
            }
        },
        mounted() {
            this.mockTableData();
            this.gateId = this.$route.query.id
        },
        methods: {
            //backJump
            backGoJump:function(){
                this.$router.back(-1)
            },
            jumpPage: function (id, url) {
                let route = '';

                if (id > 0) {
                    route = {
                        path: url,
                        query: {
                            id: id,
                        },
                    };
                } else {
                    console.log('进来2')

                    route = {
                        path: url,

                    };
                }


                this.$router.push(route);
            },
            mockTableData() {
                let that = this;

                let para = {
                    // areaId:'',
                    // current: this.current,
                    subsysid: that.$route.query.id,
                };
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.GateCtrlList, para, function (res) {
                    that.$Loading.finish();
                    // that.total = res.Msg.TotalCount;
                    // that.current = res.Msg.CurrentPage;
                    that.tableData = res.Msg;

                    console.log('tableData:', res)

                    // that.tableData.forEach(function (item, i) {
                    //     that.images.push(that.apiUtilBase + item.faceimgurl);
                    //     console.log("图片:",that.images)
                    // });
                });

            },
            // 表格内选中及取消选中操作
            tableItemChecked: function (selection, row) {

                this.selectedItems = selection;
                console.log('选择删除项:', selection);

                if (this.selectedItems.length > 1) {
                    // 此处添加不要多选的提示
                    this.$Message.warning('此处不允许批量删除 ');
                    this.deleteBtnEnable = true;

                } else {
                    this.deleteBtnEnable = false;
                }
            },
            //删除
            remove: function () {
                let self = this;
                let that = this;
                this.$Modal.confirm({
                    title: '操作警告',
                    content: '即将删除选中的门区控制器项，该操作不可逆转，请确认您的操作。',
                    onOk: function () {
                        this.$Loading.start();
                        var selectedItemIds = "";
                        for (var i = 0; i < that.selectedItems.length; i++) {
                            selectedItemIds += that.selectedItems[i].Id.toString() + ",";
                        }
                        selectedItemIds = selectedItemIds.substring(0, selectedItemIds.length - 1);
                        let para = {id: selectedItemIds};
                        let params = that.$qs.stringify(para);

                        apiUtil.post(that, apiUtil.urls.DelGateCtrl, params, function (res) {
                            that.$Message.success('删除成功。');
                            that.mockTableData();
                        });
                    }
                });
            },
            //添加
            handleSubmitAdd(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.addData(model);
                });
            },
            addData: function (model) {
                let that = this;

                model.Forgatesubsysid = that.$route.query.id;
                let params = that.$qs.stringify(model);
                this.$Loading.start();

                console.log('params', model);

                apiUtil.post(that, apiUtil.urls.AddGatesCtrl, params, function (res) {
                    that.$Loading.finish();
                    that.addModal = false;
                    that.mockTableData();
                    that.$Message.info('添加成功 ');
                    that.handleReset('formValidate')
                });
            },
            //点击编辑
            editClick: function (row) {
                this.editIsShow = true;
                this.formValidate2.controllername = row.Controllername;
                this.formValidate2.comment = row.Comment;
                this.formValidate2.isplaysound = row.Isplaysound.toString();
                this.formValidate2.controllerport = row.Controllerport;
                this.formValidate2.controllerip = row.Controllerip;
                this.formValidate2.forgatesubsysid = row.Forgatesubsysid;
                this.formValidate2.Id = row.Id;
            },
            handleSubmitEdit(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.comfirmModifyPSEdit(model);
                })
            },
            comfirmModifyPSEdit: function (model) {
                let that = this;
                let params = {};

                params = that.$qs.stringify(model);
                console.log('params:', params)
                apiUtil.post(that, apiUtil.urls.EditGateCtrl, params, function (res) {
                    that.$Message.info('修改成功 ')
                    that.mockTableData();

                });
            },
            //重置表单
            handleReset (name) {
                this.$refs[name].resetFields();
            }
        },
    }
</script>
